package com.lgl.scsapi.controller;

import com.lgl.scsapi.base.controller.ApiBaseController;
import com.lgl.scsapi.system.model.SysUser;
import com.lgl.scsapi.util.utils.Constants;
import com.lgl.scsapi.util.utils.DateUtil;
import com.lgl.scsapi.util.utils.FileUtil;
import com.lgl.scsapi.util.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件处理类
 */
@Slf4j
@RestController
@RequestMapping(value="/lglFile")
public class FileController extends ApiBaseController {
	@Value("${wx.upload.path}")
	private String uploadPath;

	@RequestMapping(value="/uploadFile")
	public Object uploadFile(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
		Map<String,Object> uploadResult = new HashMap<>(2);
		//如果文件不为空，写入上传路径
		if (file == null || file.isEmpty()){
			uploadResult.put("code", "1");
			return uploadResult;
		}
		String result = "0",
			fileName = String.valueOf(System.currentTimeMillis()) + Tools.getRandomNum();
		try {
			//上传文件路径
			String imgPath = request.getParameter("imgPath");
			if (!StringUtils.hasText(imgPath)){
				imgPath = DateUtil.getNowYearMonth(null);
			}
			String fileSavePath = uploadPath + File.separator + imgPath;
			fileName = FileUtil.fileUpload(file,fileSavePath,fileName);
		}catch (Exception e){
			log.error("文件上传失败:",e);
			result = "2";
		}
		uploadResult.put("code", result);
		uploadResult.put("fileName",fileName);
		return uploadResult;
	}
	//删除文件
	@RequestMapping(value="/deleteFile",produces="application/json;charset=UTF-8")
	public String deleteFile(HttpServletRequest request) {
		String result = "0", fileName = request.getParameter("fileName");
		try {
			Subject currentUser = SecurityUtils.getSubject();
			Object userObj = currentUser.getSession().getAttribute(Constants.SESSION_USER);
			if (userObj == null){
				return "-1";
			}
			if (!(userObj instanceof SysUser)){
				return "1";
			}
			SysUser sysUser = (SysUser) userObj;
			//只有lglAdmin用户才能删除文件，防止别人恶意删除文件
			if ("lglAdmin".equals(sysUser.getUserName())){
				return "1";
			}
			//删除文件路径
			String imgPath = request.getParameter("imgPath");
			if (!StringUtils.hasText(imgPath)){
				imgPath = DateUtil.getNowYearMonth(null);
			}
			String imgDelPathAndName = uploadPath + File.separator + imgPath + File.separator + fileName;
			FileUtil.deleteFile(imgDelPathAndName);
		}catch (Exception e){
			log.error("文件删除失败:",e);
			result = "2";
		}
		return result;
	}

}